package com.dong.response;

import com.google.common.collect.Maps;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class ResponseResult {

    @ApiModelProperty("是否成功")
    private Boolean success;
    @ApiModelProperty("状态码")
    private Integer code;
    @ApiModelProperty("返回信息")
    private String message;
    private Map<String, Object> data = Maps.newHashMap();

    /**
     * @Description 构造方法私有化，里面的方法都是私有的
     * 达到保护属性的作用
     **/
    private ResponseResult() {

    }

    /**
     * 成功
     **/
    public static ResponseResult ok() {
        ResponseResult result = new ResponseResult();
        result.setSuccess(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 失败
     **/
    public static ResponseResult error() {
        ResponseResult result = new ResponseResult();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.ERROR.getCode());
        result.setMessage(ResultCodeEnum.ERROR.getMessage());
        return result;
    }

    /***自定义返回*/
    public ResponseResult success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public ResponseResult code(Integer code) {
        this.setCode(code);
        return this;
    }

    public ResponseResult message(String message) {
        this.setMessage(message);
        return this;
    }

    public ResponseResult data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public ResponseResult data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
